﻿Public Class FormIntoRecord
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objpw_ck As New clsPWck
    Dim objGjbMst As New clsGjbMst
    Dim objFlbMst As New clsFlbMst
    Dim txt As TextBox
    Private Sub FormIntoRecord_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.AddDays(-1).ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dtPw As DataTable = objpw_ck.getPwCkInList(txtStartDate.Text.Trim() + " 00:00:00.000",
                                                              txtEndDate.Text.Trim() + " 23:59:59.999",
                                                              txtCode.Text.Trim())
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtPw
        Else
            MsgBox("请输入开始时间和结束时间", vbYes, "提示")
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        setDgvDetails()
    End Sub

    Private Sub setDgvDetails()
        Dim dtDetails As New DataTable
        If dgv.SelectedRows.Count > 0 Then
            dtDetails = objpw_ck.getPwCkInDetailsList(dgv.SelectedRows(0).Cells("ck_in_code").Value.ToString())
            dtDetails.Columns.Add("oper_type_name")
            dtDetails.Columns.Add("validity_name")
            dtDetails.Columns.Add("size")
            If dtDetails.Rows.Count > 0 Then
                For Each row As DataRow In dtDetails.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                        If DBNull.Value.Equals(row("material_code")) = False Then
                            Dim dtgjbmst As DataTable = objGjbMst.selectAll(row("material_code"))
                            row("size") = dtgjbmst.Rows(0)("size")
                        End If
                    Else
                        row("oper_type_name") = "法兰板"
                        If DBNull.Value.Equals(row("material_code")) = False Then
                            Dim dtflbmst As DataTable = objFlbMst.selectAll(row("material_code"))
                            row("size") = dtflbmst.Rows(0)("size")
                        End If
                    End If
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtDetails
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If dgv1.SelectedRows.Count > 0 Then
            If dgv1.SelectedRows(0).Cells("Col_validity").Value.ToString().Equals("1") Then
                Dim rtn As Integer = objpw_ck.updateInDetailsVlidity(dgv1.SelectedRows(0).Cells("Coloper_type").Value.ToString(), "0",
                                                                     dgv1.SelectedRows(0).Cells("Col_sys_id").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("ColBatchNo").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString(),
                                                                     dgv1.SelectedRows(0).Cells("Col_into_num").Value.ToString())
                If rtn <> -2 Then
                    MsgBox("作废成功")
                    setDgvData()
                    CType(Owner, FormMain).setDgvData()
                Else
                    MsgBox("作废失败")
                End If
            Else
                MsgBox("该入库记录已作废")
            End If
        End If
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If String.IsNullOrEmpty(rk_num.Text) Then
            MsgBox("入库数量不能为空", vbYes, "提示")
            Exit Sub
        End If
        If rk_num.Value <= 0 Then
            MsgBox("入库数量需 > 0", vbYes, "提示")
            Exit Sub
        End If
        If MsgBox("您确定要进行修改入库数量吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
            Dim batchNo As String = dgv1.SelectedRows(0).Cells("ColBatchNo").Value.ToString()
            Dim rtn As Integer = objpw_ck.update(batchNo, rk_num.Text.Trim())
            If rtn <> -2 Then
                rk_num.Value = 0
                setDgvDetails()
                MsgBox("修改入库数量成功", vbYes, "提示")
            Else
                MsgBox("修改入库数量失败", vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub rk_num_MouseDown(sender As Object, e As MouseEventArgs) Handles rk_num.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

End Class